package com.fishingwithme.android.ui.screens.my.maintain

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import com.fishingwithme.android.R
import com.fishingwithme.android.data.model.HarvestApprovalDto
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.ui.theme.AppDimensions
import com.fishingwithme.android.ui.viewmodels.MyHarvestMaintainViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyHarvestMaintainScreen(
    navController: NavController,
    viewModel: MyHarvestMaintainViewModel = hiltViewModel()
) {
    // State for approval status filter (true = approved, false = pending)
    var disabled by remember { mutableStateOf(false) }

    val harvests = viewModel.getApprovalHarvests(disabled).collectAsLazyPagingItems()

    Scaffold { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(AppDimensions.SPACING_8)
        ) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = MaterialTheme.colorScheme.background),
                shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_12)
            ) {


                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(AppDimensions.SPACING_8),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically
                ) {

                    // 切换按钮
                    OutlinedButton(
                        onClick = { disabled = !disabled },
                        modifier = Modifier.height(AppDimensions.BUTTON_HEIGHT_36)
                    ) {
                        Text(
                            text = if (disabled) stringResource(R.string.show_disabled) else stringResource(
                                R.string.show_enabled
                            ),
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                }
            }

            // Harvest list
            LazyColumn(
                verticalArrangement = Arrangement.spacedBy(AppDimensions.SPACING_8)
            ) {
                items(harvests.itemCount) { index ->
                    harvests[index]?.let { harvest ->
                        HarvestItem(harvest = harvest, navController)
                    }
                }

                // Loading states
                harvests.apply {
                    when {
                        loadState.refresh is LoadState.Loading -> {
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    CircularProgressIndicator()
                                }
                            }
                        }

                        loadState.append is LoadState.Loading -> {
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    CircularProgressIndicator()
                                }
                            }
                        }

                        loadState.refresh is LoadState.Error -> {
                            val error = (loadState.refresh as LoadState.Error).error
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(
                                        text = stringResource(R.string.error_load_failed),
                                        color = MaterialTheme.colorScheme.error
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun HarvestItem(harvest: HarvestApprovalDto, navController: NavController) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                val router = "${FishingNavigation.APPROVE_HARVEST}?id=${harvest.id}"
                navController.navigate(router)
            },
        shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_12)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppDimensions.SPACING_16)
        ) {
            // Harvest ID and basic info
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                // Approval status indicator
                val recText = if (harvest.isRec == 1) {
                    stringResource(R.string.recommend)
                } else {
                    stringResource(R.string.not_recommended)
                }

                val recColor = if (harvest.isRec == 1) {
                    MaterialTheme.colorScheme.primary
                } else {
                    MaterialTheme.colorScheme.secondary
                }

                Text(
                    text = recText,
                    style = MaterialTheme.typography.bodyMedium,
                    color = recColor
                )
            }

            Spacer(modifier = Modifier.height(AppDimensions.SPACING_4))
            // Harvest type
            Text(
                text = "${stringResource(R.string.harvest_type)}: ${harvest.harvestType.name}",
                style = MaterialTheme.typography.bodyMedium
            )

            Spacer(modifier = Modifier.height(AppDimensions.SPACING_4))

            Text(
                text = "${stringResource(R.string.score_value)}: ${harvest.scoreVal.name}",
                style = MaterialTheme.typography.bodyMedium
            )
            Spacer(modifier = Modifier.height(AppDimensions.SPACING_4))
            // status
            val statusText = if (harvest.disabled == 1) {
                stringResource(R.string.show_disabled)
            } else {
                stringResource(R.string.show_enabled)
            }

            val statusColor = if (harvest.disabled == 1) {
                MaterialTheme.colorScheme.secondary
            } else {
                MaterialTheme.colorScheme.primary
            }
            Text(
                text = statusText,
                style = MaterialTheme.typography.bodyMedium,
                color = statusColor
            )

            // status
            Spacer(modifier = Modifier.height(AppDimensions.SPACING_4))
            Text(
                text = harvest.city,
                style = MaterialTheme.typography.bodyMedium
            )
        }
    }
}